package com.jc.order.service.impl;

import com.jc.order.constant.OrderConst;
import com.jc.order.domain.KjbInsurances;
import com.jc.order.dto.JcPolicyDTO;
import com.jc.order.mapper.KjbInsurancesMapper;
import com.jc.order.service.KjbInsurancesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;


@Service
public class KjbInsurancesServiceImpl implements KjbInsurancesService {


    @Resource
    private KjbInsurancesMapper kjbInsurancesMapper;



    @Override
    public KjbInsurances selectByInsuranceTypeAndRecordId(Integer insuranceType, Long recordId) {
        Example example = new Example(KjbInsurances.class, true, true);
        example.createCriteria()
                .andEqualTo("insuranceType",insuranceType)
                .andEqualTo("recordId", recordId)
                .andEqualTo("deleted",false);


        KjbInsurances insurances =  kjbInsurancesMapper.selectOneByExample(example);

        return insurances;
    }

    @Override
    @Transactional
    public Boolean saveInsurances(KjbInsurances insurances) {

        KjbInsurances entity = this.selectByInsuranceTypeAndRecordId(insurances.getInsuranceType(),insurances.getRecordId());
        if (entity!=null){
            //更新
            insurances.setId(entity.getId());
            insurances.setCreateTime(entity.getCreateTime());
            insurances.setCreator(entity.getCreator());
            Example example = new Example(KjbInsurances.class, true, true);
             example.createCriteria()
                    .andEqualTo("insuranceType",insurances.getInsuranceType())
                    .andEqualTo("recordId", insurances.getRecordId());
            kjbInsurancesMapper.updateByExample(insurances, example);
        }else{
            //新增
            kjbInsurancesMapper.insert(insurances);
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional
    public Boolean saveInsurances(JcPolicyDTO dto) {
        KjbInsurances insurances =new KjbInsurances();
        this.toInsurances(dto,insurances);
        return this.saveInsurances(insurances);
    }






    private void toInsurances(JcPolicyDTO dto,KjbInsurances insurances){
        BeanUtils.copyProperties(dto,insurances);

        //传统货运
        if (OrderConst.PRODUCT_CODE_1.equalsIgnoreCase(dto.getProductCode())) {
            insurances.setInsuranceType(2);
        }
        //跨境大货
        if (OrderConst.PRODUCT_CODE_2.equalsIgnoreCase(dto.getProductCode())) {
            insurances.setInsuranceType(1);
        }

        insurances.setId(null);
        insurances.setRecordId(dto.getId());
        insurances.setFormCreateTime(dto.getCreateTime());
        insurances.setAdminLocker(0);
        insurances.setFirstComesIntoEffectTime(null);
        insurances.setComesIntoEffectTime(null);
//        insurances.setCreator(dto.getCreator());
//        insurances.setCreateTime(dto.getCreateTime());
//        insurances.setUpdater(dto.getUpdater());
//        insurances.setUpdateTime(dto.getUpdateTime());
//        insurances.setDeleted(dto.getDeleted());
        insurances.setPublishTime(dto.getCreateTime());

    }



}
